﻿package com.mochimedia.ui {

	import com.mochimedia.ui.*;

    import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author Geoff
	 */
	public class ASUIObject extends Cell {
		
		public function get root():Cell { return this; }
		
		private var _idMap:Object;
		public function get idMap():Object { return _idMap; }

		private var _nameMap:Object;
		public function get nameMap():Object { return _nameMap; }
		
		private var _form:Object;
		public function get form():Object { return _form; }
		
		private var _xmlString:String;
		private var _cssText:String;
		
		//
		//
		public function ASUIObject (container:Sprite, width:Number, height:Number, xmlString:String, cssText:String, position:Position = null, style:Style = null) {
			
			init_ASUIObject(container, width, height, xmlString, cssText, position, style);
			
			if (_container != null) create();
			
		}
		
		//
		//
		private function init_ASUIObject (container:Sprite, width:Number, height:Number, xmlString:String, cssText:String, position:Position = null, style:Style = null):void {
			
			super.init_Cell(container, width, height, false, false, 0, position, style);
			
			_idMap = { };
			_nameMap = { };
			_form = { };

			_width = (width > 0) ? width : (_container.width > 0) ? _container.width : mainStage.stageWidth;
			_height = (height > 0) ? height : (_container.height > 0) ? _container.height : mainStage.stageHeight; 
			
			_xmlString = xmlString;
			_cssText = cssText;
			
		}
		
		//
		//
		override public function create ():void {
			
			super.create();

			ASUIML.create(this, _xmlString, _cssText, _style);
			
		}
		
		//
		//
		override public function connect (event:String, target:Component, property:String):void {
			
			if (target.name.length > 0) {
				form[target.name] = (target.value != null) ? target.value : "";
				target.addEventListener(Component.EVENT_CHANGE, onEvent);
				target.addEventListener(Component.EVENT_CLICK, onEvent);
			}
			
		}
		
		//
		//
		public function connectHTMLField (h:HTMLField):void {
			
			var self:Object = this;
			h.addEventListener(Component.EVENT_CLICK, onEvent);

		}
		
		//
		//
		public function onEvent (e:Event):void {
			
			var c:Component = Component(e.target);
			
			if (c.name.length > 0) form[c.name] = c.value;
	 
			bubble(e);
			
		}
		
		//
		//
		private function bubble (e:Event):void {
			
			dispatchEvent(e);
			
		}
		
	}
	
}